"""Music model."""
from app import db
from app.models.base import BaseModel


class Music(BaseModel):
    """Music model representing generated background music."""
    
    __tablename__ = 'music'
    
    user_id = db.Column(db.String(36), db.ForeignKey('users.id'), nullable=False)
    session_id = db.Column(db.String(36), db.ForeignKey('walking_sessions.id'))
    poi_id = db.Column(db.String(36), db.ForeignKey('pois.id'))
    file_url = db.Column(db.String(512), nullable=False)
    duration = db.Column(db.Integer)  # seconds
    meta_info = db.Column(db.JSON, default=dict)
    
    # Relationships
    user = db.relationship('User', back_populates='music_library')
    session = db.relationship('WalkingSession')
    poi = db.relationship('POI')
    
    def to_dict(self):
        """Convert music to dictionary."""
        return {
            'id': self.id,
            'user_id': self.user_id,
            'session_id': self.session_id,
            'poi_id': self.poi_id,
            'file_url': self.file_url,
            'duration': self.duration,
            'metadata': self.meta_info,
            'created_at': self.created_at.isoformat() if self.created_at else None
        }
